package city.spring.modules.medicalstation.drugdepositDrugExt.controller;

import city.spring.modules.medicalstation.drugdepositDrugExt.entity.DrugdepositDrugExtEntity;
import city.spring.modules.medicalstation.drugdepositDrugExt.service.DrugdepositDrugExtService;
import city.spring.modules.medicalstation.drugdepositDrugExt.vo.DrugdepositDrugExtVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：药品缴存与药品关联表
 *
 * @author 谢金婷
 * @date 2020-10-13 16:56:39
 */
@RestController
@RequestMapping("/drugdeposit_drug_ext")
public class DrugdepositDrugController extends ApiController {
    private final DrugdepositDrugExtService drugdepositDrugExtService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DrugdepositDrugExtEntity, ?>> entityOrderFields;

    public DrugdepositDrugController(DrugdepositDrugExtService drugdepositDrugExtService) {
        this.drugdepositDrugExtService = drugdepositDrugExtService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DrugdepositDrugExtEntity::getId);
        entityOrderFields.add(DrugdepositDrugExtEntity::getDrugdepositId);
        entityOrderFields.add(DrugdepositDrugExtEntity::getDrugId);
        entityOrderFields.add(DrugdepositDrugExtEntity::getDrugCode);
        entityOrderFields.add(DrugdepositDrugExtEntity::getDepositquantity);
        entityOrderFields.add(DrugdepositDrugExtEntity::getSpecifications);
        entityOrderFields.add(DrugdepositDrugExtEntity::getStatus);
        entityOrderFields.add(DrugdepositDrugExtEntity::getGmtCreate);
        entityOrderFields.add(DrugdepositDrugExtEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>药品缴存与药品关联表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DrugdepositDrugExtEntity entity) {
        LambdaQueryChainWrapper<DrugdepositDrugExtEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>药品缴存与药品关联表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, DrugdepositDrugExtVO entity) {
//        LambdaQueryChainWrapper<DrugdepositDrugExtEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DrugdepositDrugExtEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DrugdepositDrugExtEntity> queryWrapper = buildQuery(entity);
        return success(drugdepositDrugExtService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }


    /**
     * 获取一个 <strong>药品缴存与药品关联表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {

        QueryWrapper<DrugdepositDrugExtVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DrugdepositDrugExtVO> records = drugdepositDrugExtService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
//        return success(drugdepositDrugExtService.getById(id));
    }


    /**
     * 添加一个 <strong>药品缴存与药品关联表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DrugdepositDrugExtEntity entity) {
        drugdepositDrugExtService.saveDrugdepositDrugExt(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }


    /**
     * 修改一个 <strong>药品缴存与药品关联表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DrugdepositDrugExtEntity entity) {
        entity.setId(id);
        drugdepositDrugExtService.updateDrugdepositDrugExt(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>药品缴存与药品关联表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        drugdepositDrugExtService.deleteDrugdepositDrugExt(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>药品缴存与药品关联表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        drugdepositDrugExtService.deleteDrugdepositDrugExt(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DrugdepositDrugExtEntity> buildLambdaQuery(DrugdepositDrugExtEntity entity) {
        LambdaQueryChainWrapper<DrugdepositDrugExtEntity> lambdaQuery = drugdepositDrugExtService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DrugdepositDrugExtEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDrugdepositId()), DrugdepositDrugExtEntity::getDrugdepositId, entity.getDrugdepositId());
        lambdaQuery.eq(entity.getDrugId() != null, DrugdepositDrugExtEntity::getDrugId, entity.getDrugId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDrugCode()), DrugdepositDrugExtEntity::getDrugCode, entity.getDrugCode());
        lambdaQuery.eq(entity.getDepositquantity() != null, DrugdepositDrugExtEntity::getDepositquantity, entity.getDepositquantity());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSpecifications()), DrugdepositDrugExtEntity::getSpecifications, entity.getSpecifications());
        lambdaQuery.eq(entity.getStatus() != null, DrugdepositDrugExtEntity::getStatus, entity.getStatus());
        lambdaQuery.eq(entity.getGmtCreate() != null, DrugdepositDrugExtEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, DrugdepositDrugExtEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DrugdepositDrugExtEntity> buildQuery(DrugdepositDrugExtVO entity) {
        QueryWrapper<DrugdepositDrugExtEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.eq(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.eq(StringUtils.isNotBlank(entity.getOldmanName()), "t3.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getDrugdepositId()), "t1.drugdeposit_id", entity.getDrugdepositId());
        wrapper.eq(entity.getDrugId() != null, "t1.drug_id", entity.getDrugId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t2.name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getDrugCode()), "t1.drug_code", entity.getDrugCode());
        wrapper.eq(entity.getDepositquantity() != null, "t1.quantity", entity.getDepositquantity());
        wrapper.like(StringUtils.isNotBlank(entity.getSpecifications()), "t1.specifications", entity.getSpecifications());
        wrapper.eq(entity.getStatus() != null, "t1.status", entity.getStatus());
        return wrapper;
    }

    /**
     * 跟据老人id查找关联的已进行用药设置的药品信息
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("/getDrugdepositDrugByOldmanId/{oldmanId}")
    public Object getDrugdepositDrugByOldmanId(@PathVariable String oldmanId) {
        return drugdepositDrugExtService.getDrugdepositDrugByOldmanId(oldmanId);
    }

    /**
     * 用药设置时，查找已经该老人已经缴存并且未进行用药设置的药品
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("/getByOldmanId/{oldmanId}")
    public Object getByOldmanId(@PathVariable String oldmanId) {
        return success(drugdepositDrugExtService.getByOldmanId(oldmanId));
    }

}
